{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "78c04082",
   "metadata": {},
   "source": [
    "## 字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "281b1950",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T04:14:51.681274Z",
     "start_time": "2021-11-14T04:14:51.666273Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Name': 'Ford Prefect',\n",
       " 'Gander': 'Male',\n",
       " 'Occupation': 'Research',\n",
       " 'Home Planet': 'Betelgeuse Seven'}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用key查找value\n",
    "person3 = {'Name': 'Ford Prefect' , 'Gander': 'Male' , 'Occupation': 'Research', 'Home Planet': 'Betelgeuse Seven'}\n",
    "person3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2a46c44",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "bdac48b4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T04:14:56.875571Z",
     "start_time": "2021-11-14T04:14:56.868571Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Name': 'Ford Prefect',\n",
       " 'Gander': 'Male',\n",
       " 'Occupation': 'Research',\n",
       " 'Home Planet': 'Betelgeuse Seven',\n",
       " 'Age': 33}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 新键\n",
    "person3['Age'] = 33\n",
    "person3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a70fa297",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T04:14:57.945632Z",
     "start_time": "2021-11-14T04:14:57.926631Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Arthur': {'Gender': 'Male',\n",
      "            'Home Planet': 'Earth',\n",
      "            'Name': 'Arthur Dent',\n",
      "            'Occupation': 'Sandwich-Maker'},\n",
      " 'Ford': {'Gender': 'Male',\n",
      "          'Home Planet': 'Betelgeuse Seven',\n",
      "          'Name': 'Ford Prefect',\n",
      "          'Occupation': 'Researcher'},\n",
      " 'Marvin': {'Gender': 'Unknown',\n",
      "            'Home Planet': 'Unknown',\n",
      "            'Name': 'Marvin',\n",
      "            'Occupation': 'Paranoid Android'},\n",
      " 'Tricia': {'Gender': 'Female',\n",
      "            'Home Planet': 'Earth',\n",
      "            'Name': 'Tricia McMillan',\n",
      "            'Occupation': 'Mathematician'}}\n"
     ]
    }
   ],
   "source": [
    "#Zhichao\n",
    "\n",
    "import pprint\n",
    "people = {}\n",
    "people['Ford'] = {\"Name\":'Ford Prefect',\n",
    "                  \"Gender\":'Male',\n",
    "                  \"Occupation\":'Researcher',\n",
    "                  \"Home Planet\":'Betelgeuse Seven'}\n",
    "people['Arthur'] = {\"Name\":'Arthur Dent',\n",
    "                  \"Gender\":'Male',\n",
    "                  \"Occupation\":'Sandwich-Maker',\n",
    "                  \"Home Planet\":'Earth'}\n",
    "people['Tricia'] = {\"Name\":'Tricia McMillan',\n",
    "                    \"Gender\":'Female',\n",
    "                    \"Occupation\":'Mathematician',\n",
    "                    \"Home Planet\":'Earth'}\n",
    "people['Marvin'] = {\"Name\":'Marvin',\n",
    "                    \"Gender\":'Unknown',\n",
    "                    \"Occupation\":'Paranoid Android',\n",
    "                    \"Home Planet\":'Unknown'}\n",
    "pprint.pprint(people)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "897db9e8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T04:14:59.208705Z",
     "start_time": "2021-11-14T04:14:59.198704Z"
    }
   },
   "outputs": [],
   "source": [
    "?pprint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "875b70a6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T04:15:00.766794Z",
     "start_time": "2021-11-14T04:15:00.761793Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found = {}\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4ad2950a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T04:15:04.705019Z",
     "start_time": "2021-11-14T04:15:04.699019Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found['a'] = 0\n",
    "found['e'] = 0\n",
    "found['i'] = 0\n",
    "found['o'] = 0\n",
    "found['u'] = 0\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c915844b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T04:15:05.948090Z",
     "start_time": "2021-11-14T04:15:05.940090Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 1, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found['e'] = found['e'] + 1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "603ed6b8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T04:15:07.772194Z",
     "start_time": "2021-11-14T04:15:07.764194Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 2, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 更新频度\n",
    "found['e'] += 1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a19511b1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T04:15:08.943261Z",
     "start_time": "2021-11-14T04:15:08.936261Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "e\n",
      "i\n",
      "o\n",
      "u\n"
     ]
    }
   ],
   "source": [
    "# for 循环处理字典时，解释器只处理字典的键\n",
    "for kv in found:\n",
    "    print(kv)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b133a8f9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T04:15:10.046324Z",
     "start_time": "2021-11-14T04:15:10.036324Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s).\n",
      "e was found 2 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "for k in found:\n",
    "    print(k, 'was found',found[k], 'time(s).') # 用k表示键，found[k]用来访问值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3cb0118c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T04:15:10.994379Z",
     "start_time": "2021-11-14T04:15:10.983378Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s).\n",
      "e was found 2 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "for k in sorted(found):\n",
    "    print(k, 'was found',found[k], 'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "a5dd36ee",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T04:16:20.726367Z",
     "start_time": "2021-11-14T04:16:20.711366Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s).\n",
      "e was found 2 time(s).\n",
      "i was found 2 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 1 time(s).\n"
     ]
    }
   ],
   "source": [
    "for k,v in sorted(found.items()):\n",
    "    print(k, 'was found', v, 'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "99495c0f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T04:15:47.122445Z",
     "start_time": "2021-11-14T04:15:37.412890Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个你要查询元音的单词justinbieber\n",
      "a was found 0 time(s).\n",
      "e was found 2 time(s).\n",
      "i was found 2 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 1 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a', 'e', 'i', 'o', 'u']\n",
    "word = input(\"请输入一个你要查询元音的单词\")\n",
    "found= {}\n",
    "found['a'] = 0\n",
    "found['e'] = 0\n",
    "found['i'] = 0\n",
    "found['o'] = 0\n",
    "found['u'] = 0\n",
    "for letter in word :\n",
    "    if letter in vowels:\n",
    "        found[letter] += 1\n",
    "for k,v in sorted(found.items()):\n",
    "    print(k, 'was found', v, 'time(s).')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdd9ca31",
   "metadata": {},
   "source": [
    "* 用 in 操作符检查一个字典中是否存在某个键"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "4a5998a8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T04:15:51.694707Z",
     "start_time": "2021-11-14T04:15:51.691706Z"
    }
   },
   "outputs": [],
   "source": [
    "fruits = {}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "441763b3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T04:15:53.524811Z",
     "start_time": "2021-11-14T04:15:53.516811Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits['apples'] = 10\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "824936a3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T04:15:56.388975Z",
     "start_time": "2021-11-14T04:15:56.378974Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'apples' in fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "d1e8915c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T04:16:01.332258Z",
     "start_time": "2021-11-14T04:16:01.318257Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 1}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if 'bananas' in fruits:\n",
    "    fruits['bananas'] += 1\n",
    "else:\n",
    "    fruits['bananas'] = 1\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "c7f9680e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T04:20:18.964994Z",
     "start_time": "2021-11-14T04:20:18.953993Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 2}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if 'bananas' in fruits:\n",
    "    fruits['bananas'] += 1\n",
    "else:\n",
    "    fruits['bananas'] = 1\n",
    "fruits"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46c11b81",
   "metadata": {},
   "source": [
    "* 用“not in”替换“in”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "a69f4ccf",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T04:27:21.021134Z",
     "start_time": "2021-11-14T04:27:21.010133Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 2, 'pears': 0}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if 'pears' not in fruits:\n",
    "    fruits['pears'] = 0\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "dd70efa3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T04:28:50.635259Z",
     "start_time": "2021-11-14T04:28:50.628259Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 2, 'pears': 2}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits['pears'] += 1\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "c337155a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T04:32:05.961431Z",
     "start_time": "2021-11-14T04:32:05.954431Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 2, 'pears': 3}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits.setdefault('pears', 0)# 初始化 setdefault替换2行if/not in语句\n",
    "fruits['pears'] += 1\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "103e24d5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T04:38:35.193694Z",
     "start_time": "2021-11-14T04:38:23.066001Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个你要查询元音的单词wedding\n",
      "e was found 1 time(s).\n",
      "i was found 1 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a', 'e', 'i', 'o', 'u']\n",
    "word = input(\"请输入一个你要查询元音的单词\")\n",
    "found= {}\n",
    "for letter in word :\n",
    "    if letter in vowels:\n",
    "        found.setdefault(letter, 0)\n",
    "        found[letter] += 1\n",
    "for k,v in sorted(found.items()):\n",
    "    print(k, 'was found', v, 'time(s).')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1ddd8b6",
   "metadata": {},
   "source": [
    "## 集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "84036e0e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T06:30:27.117594Z",
     "start_time": "2021-11-14T06:30:26.203542Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = {'a', 'e', 'e', 'i', 'o', 'u', 'u'}\n",
    "vowels\n",
    "# 用大括号包围，但集合用逗号分隔，字典用冒号分隔键和值\n",
    "# 集合不允许重复，不会维持插入数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7000302f",
   "metadata": {},
   "source": [
    "* 用set创建集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "7e8f6ecc",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-14T06:37:40.488382Z",
     "start_time": "2021-11-14T06:37:40.430378Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels2 = set('aeeiouu')\n",
    "vowels2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bd9bcf77",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-07T03:12:28.878463Z",
     "start_time": "2021-12-07T03:12:28.872463Z"
    }
   },
   "outputs": [],
   "source": [
    "vowels = set('aeiou')\n",
    "word = 'hello'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b98382b",
   "metadata": {},
   "source": [
    "* union 合并集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "20c25ed9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-07T03:13:47.713972Z",
     "start_time": "2021-12-07T03:13:47.697971Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'h', 'i', 'l', 'o', 'u'}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u = vowels.union(set(word))\n",
    "u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a59dc245",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-07T03:15:31.976936Z",
     "start_time": "2021-12-07T03:15:31.968935Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'h', 'i', 'l', 'o', 'u']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u_list = sorted(list(u))\n",
    "u_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7295ec32",
   "metadata": {},
   "source": [
    "* difference 筛选出非共有元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b283bde6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-07T03:19:17.735848Z",
     "start_time": "2021-12-07T03:19:17.728848Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'i', 'u'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = vowels.difference(set(word)) # 包含在vowels集合中但不在set（word）中的对象\n",
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "faa41690",
   "metadata": {},
   "source": [
    "* intersection 筛选出共有对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2a9087e5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-07T03:21:01.484782Z",
     "start_time": "2021-12-07T03:21:01.470782Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'o'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i = vowels.intersection(set(word))\n",
    "i"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2579b115",
   "metadata": {},
   "source": [
    "* 用集合简化vowels3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "16cb2894",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-07T03:31:13.529789Z",
     "start_time": "2021-12-07T03:31:02.387152Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请提供一个需要查询元音的单词：misunderstanding\n",
      "e\n",
      "u\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = input(\"请提供一个需要查询元音的单词：\")\n",
    "found = vowels.intersection(set(word))\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2f0a1e6",
   "metadata": {},
   "source": [
    "## 元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "35731e64",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-07T03:35:26.389252Z",
     "start_time": "2021-12-07T03:35:26.375251Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "type(vowels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "4b0c116c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-07T03:35:59.149126Z",
     "start_time": "2021-12-07T03:35:59.138125Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels2 = ('a','e','i','o','u')\n",
    "type(vowels2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "aae97cc6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-07T03:36:15.419056Z",
     "start_time": "2021-12-07T03:36:15.410056Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'i', 'o', 'u']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "df0f738c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-07T03:36:23.508519Z",
     "start_time": "2021-12-07T03:36:23.498519Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('a', 'e', 'i', 'o', 'u')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89ac3b03",
   "metadata": {},
   "source": [
    "* 元组不可变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b614ee64",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-07T03:41:23.854698Z",
     "start_time": "2021-12-07T03:41:23.845697Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'I', 'o', 'u']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels[2] = 'I' # 将列表中的第三个元素改成大写I\n",
    "vowels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b8b673a8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-07T03:41:50.660231Z",
     "start_time": "2021-12-07T03:41:50.644230Z"
    }
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-15-9b26bbe72b73>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mvowels2\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'I'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mvowels2\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "vowels2[2] = 'I'\n",
    "vowels2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "744d9ef1",
   "metadata": {},
   "source": [
    "* 只有一个对象的元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "34f21592",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-07T04:03:43.053296Z",
     "start_time": "2021-12-07T04:03:43.047295Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = ('Python')\n",
    "type(t) # 字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "ae8af5dd",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-07T04:08:31.537796Z",
     "start_time": "2021-12-07T04:08:31.529796Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t2 = ('Python',) # 元组至少包含一个逗号\n",
    "type(t2) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c6a94726",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-07T04:27:58.275530Z",
     "start_time": "2021-12-07T04:27:53.208240Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请提供一个需要查询元音的单词：jupyter\n",
      "a wsa found  0 time(s).\n",
      "e wsa found  1 time(s).\n",
      "i wsa found  0 time(s).\n",
      "o wsa found  0 time(s).\n",
      "u wsa found  1 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"请提供一个需要查询元音的单词：\")\n",
    "\n",
    "found = {}\n",
    "\n",
    "found['a'] = 0\n",
    "found['e'] = 0\n",
    "found['i'] = 0\n",
    "found['o'] = 0\n",
    "found['u'] = 0\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found[letter] += 1\n",
    "        \n",
    "for k,v in sorted(found.items()):\n",
    "    print(k, 'wsa found ', v, 'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "88bee400",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-07T04:33:27.083336Z",
     "start_time": "2021-12-07T04:33:15.821692Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请提供一个需要查询元音的单词：notebook\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "'o'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-25-2e2632dfcf53>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mletter\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mword\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mletter\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mvowels\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m         \u001b[0mfound\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mletter\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[1;36m1\u001b[0m \u001b[1;31m#没有初始化频度数\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     10\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mk\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mv\u001b[0m \u001b[1;32min\u001b[0m \u001b[0msorted\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfound\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'o'"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"请提供一个需要查询元音的单词：\")\n",
    "\n",
    "found = {}\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found[letter] += 1 #没有初始化频度数\n",
    "        \n",
    "for k,v in sorted(found.items()):\n",
    "    print(k, 'was found', v, 'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "51adaf0f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-07T04:32:39.405609Z",
     "start_time": "2021-12-07T04:32:35.162367Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请提供一个需要查询元音的单词：notebook\n",
      "e was found 1 time(s).\n",
      "o was found 3 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"请提供一个需要查询元音的单词：\")\n",
    "\n",
    "found = {}\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found.setdefault(letter, 0)\n",
    "        found[letter] += 1\n",
    "        \n",
    "for k,v in sorted(found.items()):\n",
    "    print(k, 'was found', v, 'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "df8fec68",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-07T04:34:15.638114Z",
     "start_time": "2021-12-07T04:34:11.454874Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请提供一个需要查询元音的单词：happy\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = input(\"请提供一个需要查询元音的单词：\")\n",
    "found = vowels.intersection(set(word))\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "972d1e2c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24c13c63",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4309315e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "739a91e1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac3822fa",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f27a5b3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
